/**
 * 左上角 体力 金币
 */

import addEngry from "./addEngry";
import addGold from "./addGold";
import Define from "./SdkTools/Define";
import gamePrefabMgr, { uiPreName } from "./SdkTools/gamePrefabMgr";
import gameStorage from "./SdkTools/gameStorage";
import storage from "./SdkTools/storage";

const {ccclass, property} = cc._decorator;

@ccclass
export default class TopUi extends cc.Component {

    @property(cc.Label)
    tilistr: cc.Label = null;

    @property(cc.Label)
    timestr:cc.Label = null

    @property(cc.Label)
    goldstr:cc.Label = null

    private runtime:number = 0 //0 满1亏
    // LIFE-CYCLE CALLBACKS:
    // @property(cc.Prefab)
    // addtiliprefab:cc.Prefab = null
    // @property(cc.Prefab)
    // addgoldprefab:cc.Prefab = null
    

    onLoad () {
        this.setGold()
        this.setTiLi()
        
        let addengrybtn = cc.find('tili',this.node)
        addengrybtn.on('click', this.openAddEngry, this)

        let addgoldbtn = cc.find('gold', this.node)
        addgoldbtn.on('click', this.openAddGold, this)

        gameStorage.setoffLineTime()
    }

    start () {

    }

    openAddEngry(){
        let newnode = cc.instantiate(gamePrefabMgr.Instance.uiPre[uiPreName.addEngry])
        newnode.parent = cc.director.getScene()
        let sc = newnode.getComponent(addEngry)
        sc.setTopUiSc()
        console.log('ddddddddd')
        
        // let newnode = cc.instantiate(gamePrefabMgr.Instance.uiPre[uiPreName.addEngry])
        // newnode.parent = cc.director.getScene()
        // console.log('3333333',newnode)

        // console.log('打印脚本', newnode.getComponent(addEngry))
        // let sc = newnode.getComponent(addEngry)
        // console.log('4444444', sc)
        // sc.setTopUiSc()
        // console.log('5555555')
    }

    openAddGold(){
        let newnode = cc.instantiate(gamePrefabMgr.Instance.uiPre[uiPreName.addGold])
        newnode.parent = cc.director.getScene()
        let sc = newnode.getComponent(addGold)
        sc.setTopUiSc()
    }

    /**
     * 设置gold
     */
    setGold(){
        this.goldstr.string = ''+storage.Instance.getGold()
    }

    /**
     * 设置体力
     */
    setTiLi(){
        //console.log('体力',storage.Instance.getEnergy())
        this.tilistr.string = '' + storage.Instance.getEnergy()
        
    }

    addEneryTime(){
        var self = this
        this.schedule(function(){
            storage.Instance.eneryTime--
            self.timestr.string = ''+ self.secondsFormat(storage.Instance.eneryTime) 
            //console.log('倒计时', gameConfig.eneryTime,self.timeFormat(gameConfig.eneryTime)  )
            if(storage.Instance.eneryTime<=0){
                storage.Instance.eneryTime = Define.eneryTime
                self.unscheduleAllCallbacks()
                storage.Instance.setEnergy(storage.Instance.getEnergy()+1)
                self.setTiLi()
                self.runtime = 0
                self.timestr.string = '00:00'
            }
        },1,3000,0)
    }

    /**
     * 数字转时间格式
     * @param sec 
     * @returns 
     */
    secondsFormat(sec){
        let hour = Math.floor(sec / 3600);
        let minute = Math.floor((sec - hour * 3600) / 60);
        let second = sec - hour * 3600 - minute * 60;

        let hour1 = ''
        let minute1 = ''
        let second1 = ''

        if (hour < 10) {
            hour1 = "0" + hour;
        }
        if (minute < 10) {
            minute1 = "0" + minute;
        }else{
            minute1 = "" + minute;
        }
        if (second < 10) {
            second1 = "0" + second;
        }else{
            second1 = "" + second;
        }
        //return hour + ":" + minute + ":" + second;
        return minute1 + ":" + second1;
     }

    update (dt) {
        if(storage.Instance.nowEnery>=Define.maxEnery && this.timestr.string != '已满'){
            console.log('-------------')
            this.unscheduleAllCallbacks()
            this.runtime = 0
            this.timestr.string = '已满'
            storage.Instance.eneryTime = Define.eneryTime
        }
        if(this.runtime == 1) return
        if(storage.Instance.nowEnery<Define.maxEnery){
            this.runtime = 1
            this.addEneryTime()
        }
    }

    cutSchedule(){
        this.unscheduleAllCallbacks()
    }


    
}

